var keywords_changed = false;

function node_selected(node) {  

    fetch_category_content(node);    
    
};
function update_search_link() {
    var $search_link = jq("#search-by-keywords-link");
    var search_base_url = jq("#search-by-keywords-link").attr('data-searchbaseurl');
    var keywords = jq("#keywords select").val();
    if (keywords !== null) {
        if (keywords.length > 0) {
            var query = "";
            jq.each(keywords, function(idx, kw) {
                query = query + encodeURIComponent("Subject:list")+"="+encodeURIComponent(kw) + "&";
            });
            $search_link.attr('href', search_base_url + '?' + query+encodeURIComponent("Subject_usage:ignore_empty="));
            $search_link.show();
        } else {
            $search_link.attr('href', "#");
            $search_link.hide();
        }
    } else {
        $search_link.attr('href', "#");
        $search_link.hide();
    }
};
function before_change_node() {
    if (keywords_changed) {
        if (confirm('Loose changes?')) {
            return true;
        } else {
            return false;
        }
    }
};
function fetch_category_content(node) {
    var ctitle = jq(node).find('a:first').text(); 
    
    jq.ajax({
        type: 'POST',
        url: 'vtc-fetch-contents-by-category',
        data: {
              'category':ctitle
              },
        dataType: 'JSON',
        traditional: true,
        success:function(data){
        
            // Feature detect
            if ( document.getElementById ) {
                var json_data = data;
                var obj = JSON.parse(json_data);
                
                objRow = "<table class='brainresults' id='cat_table_results' border='1'>" +
                        "<thead><tr><th>Item</th><th>Description</th></tr></thead>" +
                        "<tbody>";
                for (i=0; i<obj.items.length; i++) {
                    objRow = objRow + "<tr>" +
                            "<td><a href='" + obj.items[i].url + "'>" + obj.items[i].title + "</a></td>" +
                            "<td>" + obj.items[i].description + "</td>" +
                            "</tr>"
                }
                objRow = objRow + "</tbody></table>";
                
                var element = document.getElementById('cat_table');
                element.innerHTML = String(objRow);
                
                // Add pagination
                jq('#cat_table_results').oneSimpleTablePagination({rowsPerPage: 5});
                
            } else {
                alert('Your browser lacks the capabilities required to process this request.');
            }
        
        },
        error:function(errorThrown){
            console.log(errorThrown);
        }
    });
    
}
function fetch_search_results(currElement) {
    selected_kw = currElement.innerHTML;
    
    // Make a request to the server back-end
    jq.ajax({
        type: 'POST',
        url: 'vtc-fetch-contents-by-keyword',
        data: {
              'keyword':selected_kw
              },
        dataType: 'JSON',
        traditional: true,
        success:function(data){
            
            // Feature detect
            if ( document.getElementById ) {
                var json_data = data;
                var obj = JSON.parse(json_data);
                
                objRow = "<table class='brainresults' border='1'>" +
                        "<tbody>" +
                        "<tr><th>Item</th><th>Description</th></tr>";
                for (i=0; i<obj.items.length; i++) {
                    objRow = objRow + "<tr>" +
                            "<td><a href='" + obj.items[i].url + "'>" + obj.items[i].title + "</a></td>" +
                            "<td>" + obj.items[i].description + "</td>" +
                            "</tr>"
                }
                objRow = objRow + "</tbody></table>";
                
                //alert(obj.items.length);
                
                var element = document.getElementById('myid');
                element.innerHTML = String(objRow);
            } else {
                alert('Your browser lacks the capabilities required to process this request.');
            }
        },
        error:function(errorThrown){
            alert('error');
            console.log(errorThrown);
        }
    });
}
function selected_category(node) {
    if (node == undefined) {
        var category = jq.tree_reference('VTCTree').selected;
    } else {
        var category = jq(node);
    };
    var category_path = [];
    if (category != null) {
        category_path.push(category.attr('id'));
        category.parents('li').each(function() {
            category_path.push(jq(this).attr('id'))
        });
    };
    return category_path;
};
//Added 8-May-2012 from widget
function category_selected_deselected(node, tree_obj, deselecting) {
    var selected_keywords = [];
    jq('select#keywords option').each(function() { 
        selected_keywords.push(this.value)
    });
    var selected_categories = [];
    var node_category_str = selected_category(node).join(',');
    
    jq.ajax({
            type:'POST',
            //url:portal_url+'/vtc-list-keywords_by_categories',
            url:portal_url+"/vtc-search-content-by-keywords",
            data:{  categories: selected_categories,
                    selected: selected_keywords
                 },
            success:function(data) {                        
                        var $master = jq('select#subject_unselected');
                        $master.empty();
                        jq.each(data.keywords, function() { $master.append('<option value="'+this+'">'+this+'</option>') });    
                        
                    },
            dataType: 'json',
            traditional: true
    })
};
jq(document).ready(function () {
    var $tree = jq('ul#VTCTree');

    jq('#keywords').bind('change', function() {
        keywords_changed = true;
        var node = jq.tree_reference('VTCTree').selected;
        var ctitle = node.find('a:first').text();
        jq('#selected-category-title').text(ctitle);
        jq('#save-area').show();
        update_search_link();
    });

    jq('#save-keywords').bind('click', function() {
        // send list of selected keywords to the server
        var kws = [];
        jq("#keywords select option").each(function() {
            var selected = jq(this).attr('selected');
            if (selected || (selected == 'selected')) {
                kws.push(jq(this).text());
            }
        });
        var category = selected_category();
        if (category == null) {
            jq.jGrowl('No category selected', { life: 1500 });
        } else {
            jq.ajax({
                 type: 'POST',
                 url: "vtc-category-save-keywords",
                 data: 
                    {
                        'category_path': category,
                        'kws': kws
                    },
                 success :  
                    function(data) {
                        jq.jGrowl(data.message, { life: 1500 });
                        keywords_changed = false;
                        jq('#save-area').hide();
                        if (data.keywords.length > 0) {
                            jq("#assigned-keywords-list").html(data.keywords.join(', '));
                            jq("#assigned-keywords").show();
                        } else {
                            jq("#assigned-keywords-list").empty();
                            jq("#assigned-keywords").hide();
                        }
                    },
                 dataType: 'json',
                 traditional: true
            })
        }
    });

    $tree.tree({
                    data  : {
                      type  : "json",
                      url   : portal_url+"/@@vtc-categories-tree.json",
                      async : false
                    },
                    lang : {
                        new_node    : "New category"
                    },
                    rules: {
                        deletable  : "none",
                        renameable : "none",
                        creatable  : "none",
                        draggable  : "none",
                        droppable  : "none",
                    },
                    callback: {
                        beforechange: function(node, tree_obj) { return before_change_node() },
                        //onselect: function(node, tree_obj) { node_selected(node) },
                        onselect   : function(node, tree_obj) {
                                        node_selected(node);
                                        //category_selected_deselected(node, tree_obj, false);                                        
                                     },
                        ondeselect : function(node, tree_obj) {
                                        category_selected_deselected(node, tree_obj, true);
                                     },
                        oncreate: function(node) { jq(node).attr('rel', 'folder') },
                        onrename: function(node, lang, tree_obj, rb) {
                            old_id = node.id // may be undefined (new node)
                            new_name = jq(node).children("a:visible").text();
                            // shared code. Server determines if creating/renaming by the old_name value
                            jq.ajax({
                                 type: 'POST',
                                 url: "vtc-category-added-renamed",
                                 data: 
                                    {
                                      'category_path': selected_category(node),
                                      'old_id': old_id,
                                      'new_name': new_name
                                    },
                                 success :  
                                    function(data) {
                                        jq.jGrowl(data.msg, { life: 1500 });
                                        // set/change node id
                                        if (data.result) {
                                            node.id = data.new_id
                                        }
                                    },
                                 dataType: 'json',
                                 traditional: true
                            })
                        },
                        beforedelete: function(node, tree_obj) {
                            jq.ajax({
                                 type: 'POST',
                                 url: "vtc-category-removed",
                                 data: 
                                    {
                                      'category_path': selected_category(node)
                                    },
                                 success :  
                                    function(data) {
                                        jq.jGrowl(data.msg, { life: 3000 });
                                    },
                                 dataType: 'json',
                                 traditional: true
                            });
                            return true;
                        } 
                    }
    });
    // unassigned keywords
    jq(".unassigned-keyword").click(function(event) {
        event.preventDefault(); 
        event.stopPropagation();
        var data = jq(this).attr("data-count");
        var $item = jq(this);
        if (data===undefined) {
            jq.get('@@vtc-content-count', 
                   {
                       kw : encodeURIComponent($item.attr("data-kw"))
                   },
                   function(data) {
                       link_url = '<a target="_blank" href="'+portal_url+'/search?Subject='+encodeURIComponent($item.attr("data-kw"))+'">'+$item.attr("data-kw") + " <span>("+data+")</span>"+'</a>';
                       $item.html(link_url);
                       $item.attr("data-count", data);
                       $item.unbind('click');
                   });
        } else {
            link_url = '<a target="_blank" href="'+portal_url+'/search?Subject='+encodeURIComponent($item.attr("data-kw"))+'">'+$item.attr("data-kw") + " <span>("+data+")</span>"+'</a>';
            $item.html(link_url);
            $item.unbind('click');
        }
    });
});
            
